package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_JIU_ZHOU_REFRESH_SKILLID3_FROM_CROSS, desc = "刷新锦囊跨服回包")
public class JiuZhouRefreshSkillId3CmdFromCross extends AbstractCommand {

    private CrossJiuZhouProto.CrossJiuZhouRefreshSkillId3RespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossJiuZhouProto.CrossJiuZhouRefreshSkillId3RespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
            return;
        }
        Property cost = PropertyHelper.parseStringToProperty(config.getRefreshSkillId3Cost());

        // 取消预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        JiuZhouProto.JiuZhouRefreshSkillId3RespMsg.Builder respMsg = JiuZhouProto.JiuZhouRefreshSkillId3RespMsg.newBuilder();
        respMsg.setRet(reqMsg.getRet());
        if (reqMsg.getRet() == 0) {
            // 扣除道具
            player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouRefreshSkillId3Cost);

            respMsg.addAllChooseSkillId3(reqMsg.getChooseSkillId3List());
            respMsg.setRefreshTimes(reqMsg.getRefreshTimes());
        }
        player.sendPacket(ClientProtocol.U_JIU_ZHOU_REFRESH_SKILLID3, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
